﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Systemet.Domain;

namespace Systemet
{
    public partial class frmLogin : Form
    {
        private LoginController login;
        private string anvdID;
        private string pass;

        public frmLogin()
        {
            InitializeComponent();
            login = new LoginController();
            login.setLoginForm(this);

            chechTextboxesLogin();
        }

        private void chechTextboxesLogin()
        {
            buttonLogin.Enabled = login.validateTextLength(txtboxUsername.Text, txtboxPass.Text);
        }

        private void validateLogin()
        {
            anvdID = txtboxUsername.Text;
            pass = txtboxPass.Text;

            if (login.validateLogin(anvdID, pass))
            {
                //this.Hide();
                login.changeFormToMain();
            }
            else
            {
                MessageBox.Show("Fel användarnamn eller lösenord.");
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            validateLogin();
        }

        private void buttonGenerelLogin_Click(object sender, EventArgs e)
        {
            login.validateLogin();
            //this.Hide();
            login.changeFormToMain();
        }

        private void txtboxUsername_TextChanged(object sender, EventArgs e)
        {
            buttonLogin.Enabled = login.validateTextLength(txtboxUsername.Text, txtboxPass.Text);
        }

        private void txtboxPass_TextChanged(object sender, EventArgs e)
        {
            buttonLogin.Enabled = login.validateTextLength(txtboxUsername.Text, txtboxPass.Text);
        }

        private void txtbox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (login.validateTextLength(txtboxUsername.Text, txtboxPass.Text))
                {
                    validateLogin();
                }
            }
        }

        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
